package com.giggle.nacos.discovery;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceInstance;
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.giggle.common.helper.ReflectHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.client.ServiceInstance;

import java.util.*;

/**
 * @author guozichen
 * @ClassName:
 * @Description: (这里用一句话描述累的作用)
 * @date 2020/8/26 16:58
 */
public class NacosDiscoveryClient extends com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient {

    public NacosDiscoveryClient(NacosDiscoveryProperties discoveryProperties) {
        super(new NacosServiceDiscovery(discoveryProperties));
    }

    @Override
    public List<ServiceInstance> getInstances(String serviceId) {
        try {
            List<Instance> instances = null;
            NacosDiscoveryProperties discoveryProperties = this.getDiscoveryProperties();
            if (discoveryProperties != null) {
                String group = this.getDiscoveryProperties().getGroup();
                if (StringUtils.isNotBlank(serviceId)) {
                    instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, group, true);
                    if (CollectionUtils.isEmpty(instances) && !serviceId.equals(group)) {
                        instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, serviceId, true);
                    }
                }
            }

            return hostToServiceInstanceList(instances, serviceId);
        } catch (Exception var5) {
            throw new RuntimeException("Can not get hosts from nacos server. serviceId: " + serviceId, var5);
        }
    }

    private NacosDiscoveryProperties getDiscoveryProperties() {
        NacosDiscoveryProperties discoveryProperties = (NacosDiscoveryProperties)ReflectHelper.getFieldValue(this, "discoveryProperties");
        return discoveryProperties;
    }


    public static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) {
        if (instance != null && instance.isEnabled() && instance.isHealthy()) {
            NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
            nacosServiceInstance.setHost(instance.getIp());
            nacosServiceInstance.setPort(instance.getPort());
            nacosServiceInstance.setServiceId(serviceId);
            Map<String, String> metadata = new HashMap();
            metadata.put("nacos.instanceId", instance.getInstanceId());
            metadata.put("nacos.weight", instance.getWeight() + "");
            metadata.put("nacos.healthy", instance.isHealthy() + "");
            metadata.put("nacos.cluster", instance.getClusterName() + "");
            metadata.putAll(instance.getMetadata());
            nacosServiceInstance.setMetadata(metadata);
            if (metadata.containsKey("secure")) {
                boolean secure = Boolean.parseBoolean((String)metadata.get("secure"));
                nacosServiceInstance.setSecure(secure);
            }

            return nacosServiceInstance;
        } else {
            return null;
        }
    }

    public static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances, String serviceId) {
        List<ServiceInstance> result = new ArrayList(instances.size());
        Iterator var3 = instances.iterator();

        while(var3.hasNext()) {
            Instance instance = (Instance)var3.next();
            ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId);
            if (serviceInstance != null) {
                result.add(serviceInstance);
            }
        }

        return result;
    }
}
